package com.wt.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.entity.WqunTag;
import com.wt.admin.entity.WqunUserSetUp;
import com.wt.admin.service.WqunTagService;
import com.wt.admin.service.WqunUserSetUpService;
import com.wt.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户设置表 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-09-22
 */
@Api(tags = "用户设置模块")
@RestController
@RequestMapping("/wqunUserSetUp")
@CrossOrigin
public class WqunUserSetUpController {

    @Autowired
    private WqunUserSetUpService wqunUserSetUpService;

    //查询社区管理设置
    @ApiOperation("查询社区管理设置")
    @GetMapping("/findAllCommunity/{id}")
    @PreAuthorize("hasAnyAuthority('wqunUserSetUp:findAllCommunity')")
    public R findAllCommunity(@PathVariable("id") String id){
        QueryWrapper<WqunUserSetUp> queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid",id);
        queryWrapper.select("community_total","community_update_time","community_update_automatic","community_open_user_list");
        WqunUserSetUp findAllCommunity = wqunUserSetUpService.getOne(queryWrapper);
        if(findAllCommunity == null){
            WqunUserSetUp wqunUserSetUp = new WqunUserSetUp();
            wqunUserSetUp.setUid(id);
            boolean save = wqunUserSetUpService.save(wqunUserSetUp);
            queryWrapper.select("community_total","community_update_time","community_update_automatic","community_open_user_list");
            findAllCommunity = wqunUserSetUpService.getOne(queryWrapper);
        }
        return R.ok().data("findAllCommunity",findAllCommunity);
    }

    //社区用户信息修改
    @ApiOperation("查询社区管理设置")
    @PostMapping("/updateByCommunity")
    @PreAuthorize("hasAnyAuthority('wqunUserSetUp:updateByCommunity')")
    public R updateByCommunity(@RequestBody WqunUserSetUp wqunUserSetUp){
        QueryWrapper<WqunUserSetUp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",wqunUserSetUp.getUid());
        int existUser = wqunUserSetUpService.count(queryWrapper);
        if(existUser>0){//存在该用户
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid",wqunUserSetUp.getUid());
            boolean update = wqunUserSetUpService.update(wqunUserSetUp,queryWrapper);
            return update?R.ok().message("修改成功"):R.error().message("修改失败");
        }else{//不存在该用户
            boolean save = wqunUserSetUpService.save(wqunUserSetUp);
            return save?R.ok().message("修改成功"):R.error().message("修改失败");
        }
    }
}

